import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { LocalStorage } from './local.strategy';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from 'src/user/entities/user.entity';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { JwtStorage } from './jwt.strategy';
import { UserModule } from 'src/user/user.module';
import { UserService } from 'src/user/user.service';

// 这里不建议将秘钥写死在代码也， 它应该和数据库配置的数据一样，从环境变量中来
// const jwtModule = JwtModule.register({
//   secret: 'test123456',
//   signOptions: { expiresIn: '4h' },
// })

const jwtModule = JwtModule.registerAsync({
  inject: [ConfigService],
  useFactory: async (configService: ConfigService) => {
    return {
      secret: configService.get('JWT_SECRET'),
      signOptions: { expiresIn: '4h' },
    }
  }
})

@Module({
  imports: [TypeOrmModule.forFeature([UserEntity]), UserModule, PassportModule, jwtModule],
  controllers: [AuthController],
  providers: [UserService, AuthService, LocalStorage, JwtStorage], // 导入服务
  exports: [jwtModule]
})
export class AuthModule {}
